
package cn.edu.zhku.citrusmonitor.baseinfo.pest.service;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;

import com.mysql.jdbc.Connection;

import cn.edu.zhku.citrusmonitor.baseinfo.pest.dao.PestDao;
import cn.edu.zhku.citrusmonitor.baseinfo.pest.dao.PestDaoImpl;
import cn.edu.zhku.citrusmonitor.domain.Bean;
import cn.edu.zhku.citrusmonitor.domain.TbPest;
import cn.edu.zhku.citrusmonitor.util.Message;
import cn.edu.zhku.citrusmonitor.util.UUIDUtils;
import cn.edu.zhku.citrusmonitor.util.xy.PageBean;
import net.sf.json.JSONObject;

public class PestServiceImpl implements PestService{
	PestDao PD = new PestDaoImpl();

	@Override
	public PageBean<Bean> findPestPageBean(Integer currentPage, Integer pageSize) {
		long count = 0;
		List<Bean> list = null;
		PageBean<Bean> pbn = new PageBean<>();
		try {
			count = PD.getCount();
			list = PD.getPestBeanList(currentPage, pageSize);
			pbn.setList(list);
			pbn.setCurrentPage(currentPage);//当前页
			pbn.setPageSize(pageSize);		//每页条数
			pbn.setCount(count);;		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pbn;
	}

	@Override
	public PageBean<TbPest> findAllPestDetail(Integer currentPage, Integer pageSize) {
		long count = 0;
		List<TbPest> list = null;
		PageBean<TbPest> pbn = new PageBean<>();
		try {
			count = PD.getCount();
			list = PD.getPestDetailBeanList(currentPage, pageSize);
			pbn.setList(list);
			pbn.setCurrentPage(currentPage);//当前页
			pbn.setPageSize(pageSize);		//每页条数
			pbn.setCount(count);;		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pbn;
	}

	@Override
	public JSONObject modifyPest(TbPest pest) {
		try {
			if(PD.modifyPestByPestBean(pest) != 0)
			return JSONObject.fromObject(new Message("1","修改成功"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return JSONObject.fromObject(new Message("2","修改失败"));
	}

	@Override
	public JSONObject removePest(TbPest pest) {
		try {
			if(PD.delectPestByPestBean(pest) != 0) {
				return JSONObject.fromObject(new Message("1","删除成功"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return JSONObject.fromObject(new Message("2","删除失败"));
	}

	@Override
	public JSONObject addPest(TbPest pest) {
		pest.setPestId(UUIDUtils.getId());	//给新增的记录写下唯一的ID
		try {
			if(PD.addPestByPestBean(pest) != 0) {
				return JSONObject.fromObject(new Message("1","添加成功"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return JSONObject.fromObject(new Message("2","添加失败"));
	}

	@Override
	public List<TbPest> getPest(String pestId) {
		List<TbPest> list = null;
		try {
			list = PD.getPestById(pestId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@Override
	public Bean showPest(String id) {
		Bean bean = null;
		try {
			bean = PD.showPestById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bean;
	}

	@Override
	public List<Bean> findRecentPest(Integer pageSize, Bean bean) {
		List<Bean> list = null;
		try {
			list = PD.getRecentPestBeanList(pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public PageBean<TbPest> findAllPestDetailByWord(Integer currentPage, Integer pageSize, String word) {
		List<TbPest> list = null;
		Long count = null;
		PageBean<TbPest> pbn = new PageBean<>();
		pbn.setCurrentPage(currentPage);//当前页
		pbn.setPageSize(pageSize);		//每页条数
		if(word != "") {
			try {
				count  = PD.searchPestCountByWord(word);
				list = PD.searchPestByWord(currentPage, pageSize, word);
			}catch (SQLException e) {
				e.printStackTrace();
			}
			pbn.setList(list);
			pbn.setCount(count);		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
		}else {
			count = (long) 0;
			pbn.setList(list);
			pbn.setCount(count);		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
		}
		return pbn;
	}
	
	public Long findAllPestDetailCountByWord(String word) {
		Long l = null;
		try {
			l = PD.searchPestCountByWord(word);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return l;
	}

	@Override
	public JSONObject delSelect(String[] ids){
		Connection conn = null;
		try {
			if(PD.delSelect(ids).length > 0) {
				return JSONObject.fromObject(new Message("1","删除成功"));
			}
		} catch (SQLException e) {
			try {
				DbUtils.rollback(conn);	//进行事务回滚
				} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				DbUtils.close(conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return JSONObject.fromObject(new Message("2","删除成功"));
	}
}
